package edu.cicese.alz.tests;

import edu.cicese.alz.util.IncenseUtil;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.LookupPaintScale;
import org.jfree.chart.renderer.xy.XYBlockRenderer;
import org.jfree.chart.title.PaintScaleLegend;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 10/07/12
 * Time: 05:02 PM
 */
public class BlockTest extends ApplicationFrame {

	public BlockTest(String path) {
		super("Accelerometer Chart");

		JPanel localJPanel = new ChartPanel(createIntensityChart(createDatasetFromCountFiles(path)));
//		JPanel localJPanel = new ChartPanel(createIntensityChart(createDatasetFromFiles(path)));
		localJPanel.setPreferredSize(new Dimension(1500, 750));
		setContentPane(localJPanel);
	}

	private JFreeChart createIntensityChart(XYZDataset xyzDataset) {
		DateAxis localDateAxis = new DateAxis("Date");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		localDateAxis.setTickMarkPosition(DateTickMarkPosition.START);
		localDateAxis.setDateFormatOverride(sdf);
		localDateAxis.setLowerMargin(0.0D);
		localDateAxis.setUpperMargin(0.0D);
		localDateAxis.setInverted(true);

//		DateAxis yAxis = new DateAxis("Acquisition date");
//		SimpleDateFormat at = new SimpleDateFormat("yyyy/MM/dd hh:mm");
//		yAxis.setDateFormatOverride(at);
//		yAxis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR,12, at));
//		yAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
//		yAxis.setLowerMargin(0.0);
//		yAxis.setUpperMargin(0.0);

		DateAxis localDateAxis2 = new DateAxis("Date");
		SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
		localDateAxis2.setDateFormatOverride(sdf2);
		sdf2.setTimeZone(TimeZone.getTimeZone("GMT"));
//		localDateAxis2.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1, sdf2));
		localDateAxis2.setLowerMargin(0.0D);
		localDateAxis2.setUpperMargin(0.0D);
//		NumberAxis localNumberAxis = new NumberAxis("Hour");
//		localNumberAxis.setUpperMargin(0.0D);
//		NumberFormat numberFormat = NumberFormat.getInstance();
//		localNumberAxis.setNumberFormatOverride(numberFormat);
//		localNumberAxis.setTickUnit(new NumberTickUnit(60));
//		localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		XYBlockRenderer localXYBlockRenderer = new XYBlockRenderer();
		localXYBlockRenderer.setBlockWidth(86400000.0D);
		localXYBlockRenderer.setBlockHeight(60000.0D);
		localXYBlockRenderer.setBlockAnchor(RectangleAnchor.BOTTOM_LEFT);


		XYPlot localXYPlot = new XYPlot(xyzDataset, localDateAxis, localDateAxis2, localXYBlockRenderer);
		localXYPlot.setRangeGridlinesVisible(true);
		localXYPlot.setOrientation(PlotOrientation.HORIZONTAL);
		localXYPlot.setBackgroundPaint(Color.WHITE);
		localXYPlot.setRangeGridlinePaint(Color.WHITE);
		localXYPlot.setAxisOffset(new RectangleInsets(5.0D, 5.0D, 5.0D, 5.0D));

		JFreeChart localJFreeChart = new JFreeChart("Activity Intensity Chart", localXYPlot);
		localJFreeChart.removeLegend();
		localJFreeChart.setBackgroundPaint(Color.WHITE);

		/*SymbolAxis localSymbolAxis = new SymbolAxis(null, new String[]{"", "No Data", "Sedentary", "Low Light", "High Light", "Low Moderate", "High Moderate", "Hard"});
					localSymbolAxis.setRange(0.5D, 6.5D);*/
		LookupPaintScale localLookupPaintScale = new LookupPaintScale(0.5, 6.5, Color.RED);
		localLookupPaintScale.add(0.5, Color.LIGHT_GRAY);
		localLookupPaintScale.add(1.5, new Color(151, 205, 255));
		localLookupPaintScale.add(2.5, Color.WHITE);
		localLookupPaintScale.add(3.5, new Color(220, 243, 148));
		localLookupPaintScale.add(4.5, new Color(38, 173, 31));
		localLookupPaintScale.add(5.5, new Color(255, 107, 36));
		localXYBlockRenderer.setPaintScale(localLookupPaintScale);

		SymbolAxis localSymbolAxis = new SymbolAxis(null, new String[]{"", "No Data", "No Movement", "Sedentary", "Light", "Moderate", "Hard"});
		localSymbolAxis.setRange(0.5D, 6.5D);
		localSymbolAxis.setPlot(new PiePlot());
		localSymbolAxis.setGridBandsVisible(false);

		PaintScaleLegend localPaintScaleLegend = new PaintScaleLegend(localLookupPaintScale, localSymbolAxis);
		localPaintScaleLegend.setMargin(new RectangleInsets(3.0D, 10.0D, 3.0D, 10.0D));
		localPaintScaleLegend.setPosition(RectangleEdge.BOTTOM);
		localPaintScaleLegend.setAxisOffset(5.0D);

		localJFreeChart.addSubtitle(localPaintScaleLegend);

		ChartUtilities.applyCurrentTheme(localJFreeChart);

		return localJFreeChart;
	}

	private XYZDataset createDatasetFromCountFiles(String path) {
		DefaultXYZDataset localDefaultXYZDataset = new DefaultXYZDataset();

		File dir = new File(path);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(".ct");
			}
		};
		File[] files = dir.listFiles(filter);

		if (files != null) {
			double[][] values = new double[3][1440 * files.length];
			int day = 0;
			double intensity;

			for (File file : files) {
				try {
					double[][] valuesAux = (double[][]) IncenseUtil.loadData(file.getCanonicalPath());
					for (int i = 0; i < 1440; i++) {
						values[0][day * 1440 + i] = valuesAux[0][i];
//						values[1][day * 1440 + i] = valuesAux[1][i];
						values[1][day * 1440 + i] = i * 60000;
						intensity = getIntensity(valuesAux[2][i]);
						values[2][day * 1440 + i] = intensity;
					}
					day++;

				} catch (IOException e) {
					e.printStackTrace();
				}


			}
			localDefaultXYZDataset.addSeries("Series 1", values);
		}

		return localDefaultXYZDataset;
	}

	private double getIntensity(double count) {
		if (count == -1) {
			//no data
			return 0.5;
		}
		if (count <= 50) {
			//sedentary
			return 1.5;
		}
		if (count <= 100) {
			//sedentary
			return 2.5;
		}
		if (count <= 1952) {
			//light
			return 3.5;
		}
		if (count <= 5724) {
			//moderate
			return 4.5;
		}
		//hard
		return 5.5;
	}

	public static void main(String[] a) {
		new BlockTest("D:\\_proyectoAlz\\cts").setVisible(true);
	}
}
